package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.AllTheException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.sky.constant.MessageConstant.SETMEAL_ENABLE_FAILED;
import static com.sky.constant.MessageConstant.SETMEAL_ON_SALE;
import static com.sky.constant.StatusConstant.DISABLE;
import static com.sky.constant.StatusConstant.ENABLE;

/**
 * @Author：XueShiHao
 * @Date：2025/6/6 星期五 10:37:52
 * @Email:747278007@qq.com
 * @Description:
 **/
@Service
@Slf4j

public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
/**
     * 新增套餐
     *在新增套餐的同时需要插入对应的菜品
 * 首先保存套餐的信息。然后用foreach方法遍历循环设置菜品好欢喜的id
 * 然后调用方法保存两者之间的关系
     * @param setmealDTO
     */
    @Override
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);//向套餐表中插入数据
        long id=setmeal.getId();
       List<SetmealDish> setmealDishes=setmealDTO.getSetmealDishes();
       setmealDishes.forEach(setmealDish -> {
      setmealDish.setSetmealId(id);//获取套餐的id
       });
setmealDishMapper.insertBatch(setmealDishes);
//保存两者之间的关系

    }

    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setmealMapper.pageQuery(setmealPageQueryDTO);

       return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除套餐
     *1.起售中的菜品不可以删除
     * 2.删除套餐，需要删除套餐关系对应的数据
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
ids.forEach(id->{
    Setmeal setmeal=setmealMapper.getById(id);
    if(setmeal.getStatus()== ENABLE) {
        throw new AllTheException(SETMEAL_ON_SALE);
    }
});
ids.forEach(id->{
    setmealMapper.deleteById(id);//删除套餐的数据信息
    setmealDishMapper.deleteBySetmaleId(id);//删除套餐关系的数据
});
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getByIdWithDish(Long id) {
SetmealVO setmealVO=new SetmealVO();
Setmeal setmeal=setmealMapper.getById(id);
BeanUtils.copyProperties(setmeal,setmealVO);
List<SetmealDish> setmealDishList =setmealDishMapper.getBySetmealId(id);
setmealVO.setSetmealDishes(setmealDishList);
    return setmealVO;
    }

    /**
     * 修改套餐
     *修改套餐，先调用update方法更新菜品
     * 然后删除套餐对应的菜品信息
     * 然后再重新插入菜品信息
     *
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
Setmeal setmeal=new Setmeal();
BeanUtils.copyProperties(setmealDTO,setmeal);
setmealMapper.update(setmeal);
//然后删除套餐对应的菜品信息

        setmealDishMapper.deleteBySetmaleId(setmeal.getId());
        //重新 插入菜品对应的菜品信息
        List<SetmealDish> setmealDishes=setmealDTO.getSetmealDishes();
 setmealDishes.forEach(setmealDish -> {
     setmealDish.setSetmealId(setmeal.getId());
 });
 setmealDishMapper.insertBatch(setmealDishes);
        }



    /**
     * 套餐起售停售
     *1.套餐起售时，如果关联的菜品禁售，则套餐无法起售

     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
//        起售套餐时，判断套餐内是否有停售菜品，有停售菜品提示"套餐内包含未启售菜品，无法启售"
        if (status == StatusConstant.ENABLE) {
            List<Dish> dishList = dishMapper.getBySetmealId(id);
            if (dishList != null && dishList.size() > 0) {
                dishList.forEach(dish -> {
                    if (StatusConstant.DISABLE == dish.getStatus()) { // 有停售商品
                        throw new AllTheException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }

        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }
    /**
     * 条件查询
     *
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {

 return setmealMapper.list(setmeal);
    }

    /**
     * 根据id查询菜品选项
     *
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {

     return setmealMapper.getDishItemBySetmealId(id);
    }
}
